# coding: UTF-8

import sys
import os
import re

from YTSTools import file
from YTSTools import convert
from YTSTools import font_format

class ResolverModuleAction:
    def __init__(self):
        pass

    def Method(self, template_content, ModuleActionResult):
        temps = re.split(r'({%\s*[\w\.]+\s*%})', template_content)
        slist = []
        for item in temps:
            span = re.search(r'{%\s*([\w]+)\.([\w\.]+)\s*%}', item)
            if span is None:
                slist.append(item)
                continue
            module = span[1]
            args = span[2]
            result = ModuleActionResult(module, args)
            if result is None:
                slist.append(item)
                continue
            result = convert.trimEnd(result, symbol=r'[,\s]+')
            slist.append(result)
        return ''.join(slist)

    def dict_extract_value(self, dict, args, defalutValue):
        args = str(args)
        arglist = re.split(r'\.', args)
        vdict = dict
        for arg in arglist:
            v = vdict.get(arg, None)
            if v is None:
                return defalutValue
            if 'dict' in str(type(v)):
                vdict = v
                continue
            return v
        return defalutValue
